package com.orange.adminapi.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.adminapi.pojo.req.admin.AdminChangePasswordReq;
import com.orange.adminapi.pojo.req.admin.AdminSearchReq;
import com.orange.adminapi.service.admin.AdminService;
import com.orange.core.pojo.BR;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.admin.Admin;
import com.orange.core.pojo.enums.EStatus;
import com.orange.core.util.PrincipalUtil;
import com.orange.core.validator.group.Update;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin")
public class AdminController {

    private final AdminService adminService;

    /**
     * 分页查询
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).ADMIN_LIST.permission.identifier)")
    @PostMapping("/page")
    public R<Page<Admin>> page(@RequestBody PageReqDto<AdminSearchReq> req) {
        return R.ok(adminService.adminPage(req));
    }

    /**
     * 详情
     */
    @GetMapping("/{id}")
    public R<Admin> detail(@PathVariable(value = "id", required = false) Admin admin) {
        return R.ok(adminService.detail(admin));
    }

    /**
     * 新增
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).ADMIN_ADD.permission.identifier)")
    @PostMapping
    public R<Boolean> save(@RequestBody @Validated Admin admin) {
        return BR.result(adminService.save(admin.withId(null)));
    }

    /**
     * 编辑
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).ADMIN_EDIT.permission.identifier)")
    @PutMapping
    public R<Boolean> update(@RequestBody @Validated(Update.class) Admin admin) {
        return BR.result(adminService.update(admin));
    }

    /**
     * 修改状态
     *
     * @param status 状态：0禁用，1启用
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).ADMIN_STATUS.permission.identifier)")
    @PostMapping("/{id}/status/{status}")
    public R<Boolean> setStatus(@PathVariable int id, @PathVariable(value = "status", required = false) EStatus status) {
        return BR.result(adminService.setStatus(id, status));
    }

    /**
     * 修改密码
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).ADMIN_CHANGE_PASSWORD.permission.identifier)")
    @PostMapping("/changePassword")
    public R<Boolean> changePassword(@RequestBody @Validated AdminChangePasswordReq req) {
        return adminService.changePassword(req);
    }

    /**
     * 修改自己的密码
     */
    @PostMapping("/modifyPassword")
    public R<Boolean> modifyPassword(@RequestBody @Validated AdminChangePasswordReq req) {
        req.setId(PrincipalUtil.getId());
        return adminService.changePassword(req);
    }
}
